﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Dolany.UtilityTool;
using Missbot.AIModule.Pet.PetAgainst;

namespace Missbot.AIModule.Pet
{
    /// <summary>
    /// 宠物扩展
    /// </summary>
    public static class PetExtent
    {
        public static readonly string[] AllAttributes = { "钢铁", "海洋", "深渊", "自然", "神秘" };

        #region Svcs

        private static PetSkillSvc PetSkillSvc => AutofacSvc.Resolve<PetSkillSvc>();
        private static PetLevelSvc PetLevelSvc => AutofacSvc.Resolve<PetLevelSvc>();

        #endregion

        /// <summary>
        /// 获取经验
        /// </summary>
        /// <param name="pet"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static string ExtGain(this PetRecord pet, int exp)
        {
            var remainExp = pet.Exp + exp;
            var lvl = pet.Level;
            var levelMode = PetLevelSvc[lvl];
            while (levelMode.Exp <= remainExp)
            {
                remainExp -= levelMode.Exp;
                lvl++;
                levelMode = PetLevelSvc[lvl];
            }

            var msgList = new List<string> { $"{pet.Name}获得了 {exp} 点经验值！" };
            if (lvl > pet.Level)
            {
                var points = (lvl - pet.Level) * 2;
                msgList.Add($"恭喜{pet.Name}升到了 {lvl} 级！");
                msgList.Add($"{pet.Name}获得了 {points} 点技能点！");

                pet.RemainSkillPoints += points;

                var skills = PetSkillSvc.AllSkills.Where(p => p.LearnLevel > pet.Level && p.LearnLevel <= lvl).ToList();

                if (!skills.IsNullOrEmpty())
                {
                    msgList.Add($"恭喜{pet.Name}学会了新技能 {string.Join(",", skills.Select(p => p.Name))}!");
                    pet.Skills ??= new Dictionary<string, int>();
                    foreach (var skill in skills)
                    {
                        pet.Skills.Add(skill.Name, 1);
                    }
                }
            }

            pet.Level = lvl;
            pet.Exp = remainExp;
            pet.Update();

            return string.Join("\n", msgList);
        }
    }
}